package net.lgy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lgy.controller.req.AccountLoginReq;
import net.lgy.controller.req.AccountRegisterReq;
import net.lgy.dto.AccountDTO;
import net.lgy.interceptor.LoginInterceptor;
import net.lgy.service.AccountService;
import net.lgy.util.JsonData;
import net.lgy.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/api/account/v1")
@Tag(name = "账户管理接口", description = "提供用户注册、登录、头像上传及信息查询等功能")
public class AccountController {


    @Autowired
    private AccountService accountService;

    /**
     * 注册接口
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册接口", description = "用户通过手机号等信息完成注册")
    public JsonData register(@RequestBody @Parameter(description = "注册请求参数", required = true) AccountRegisterReq req){
        accountService.register(req);
        return JsonData.buildSuccess();
    }

    /**
     * 头像上传接口
     */
    @PostMapping("/upload_avatar")
    @Operation(summary = "头像上传接口", description = "上传用户头像并返回头像访问URL")
    public JsonData uploadAvatar(@RequestParam("file") @Parameter(description = "头像文件", required = true) MultipartFile file){

        String url = accountService.uploadAvatar(file);

        return JsonData.buildSuccess(url);
    }


    /**
     * 登录模块
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录接口", description = "用户通过手机号和密码登录，成功后返回JWT令牌")
    public JsonData login(@RequestBody @Parameter(description = "登录请求参数", required = true) AccountLoginReq req){

        AccountDTO accountDTO = accountService.login(req);

        //生成token jwt ssm  一般前端存储在localStorage里面，或 sessionStorage里面
        String token = JwtUtil.geneLoginJWT(accountDTO);
        insertAiPanToken(token, accountDTO);
        return JsonData.buildSuccess(token);
    }


    /**
     * 获取用户详情接口
     */
    @GetMapping("/detail")
    @Operation(summary = "获取用户详情接口", description = "获取当前登录用户的详细信息")
    public JsonData detail(){
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        String phone = LoginInterceptor.threadLocal.get().getPhone();
        AccountDTO accountDTO = accountService.queryDetail(accountNo,phone);

        return JsonData.buildSuccess(accountDTO);
    }

    /**
     * 根据手机号查找信息
     */
    @PostMapping("/find_by_phone")
    @Operation(summary = "根据手机号查询用户信息", description = "通过手机号查询用户相关信息")
    public JsonData findByPhone(@RequestParam("phone") @Parameter(description = "用户手机号", required = true) String phone){
        AccountDTO accountDTO = accountService.findByPhone(phone);
        return JsonData.buildSuccess(accountDTO);
    }

//    将Ai盘的token插入数据库
    @PostMapping("/insert_ai_pan_token")
    @Operation(summary = "将Ai盘的token插入数据库", description = "将Ai盘的token插入数据库")
    public int insertAiPanToken(String token,AccountDTO accountDTO){
        String phone = accountDTO.getPhone();
        int rows = accountService.insertAiPanToken(phone,token);

        return rows;
    }

    /**
     * 根据手机号查找信息
     */
    @PostMapping("/find_by_account_no")
    @Operation(summary = "根据手机号查询用户信息", description = "通过手机号查询用户相关信息")
    public JsonData findByAccountNo(@RequestParam("account_no") @Parameter(description = "用户手机号", required = true) Long accountNo){
        AccountDTO accountDTO = accountService.findByAccountNo(accountNo);
        return JsonData.buildSuccess(accountDTO);
    }

}
